'''
Created on 17.08.2011

@author: osman
'''

import opf
from pets_ontology import Pets, Pet, Cat
from people_ontology import People, Person

@People.atomic
def isCloseRelativeOf(human1: Person, human2: Person) -> 'is close relative of':
    return True

@Pets.atomic
def hasOwner(pet: Pet, owner: Person) -> 'has owner':
    return True

@Pets
def hasCoOwner(pet: Pet, owner: opf.exists(Person), coOwner: Person) -> 'has co-owner':
    hasOwner(pet, owner), isCloseRelativeOf(owner, coOwner)

def realizerExample():
    print("\n>>realizerExample")
    family = opf.Ontology()
    with family:
        Tiger = Cat("Tiger")
        Robert = Person("Robert")
        Mary = Person("Mary")
        hasOwner(Tiger, Robert)
        isCloseRelativeOf(Robert, Mary)
    @opf.realizer(hasCoOwner, coOwner = Mary)
    def onCoOwner(pet, predicate, coOwner):
        print(pet, hasCoOwner, coOwner)
    family.realize(onCoOwner)

def inClassRealizerExample():
    print("\n>>inClassRealizerExample")
    family = opf.Ontology()
    with family:
        Tiger = Cat("Tiger")
        Robert = Person("Robert")
        Mary = Person("Mary")
        hasOwner(Tiger, Robert)
        isCloseRelativeOf(Robert, Mary)
    class MyRealizers:
        @opf.realizer(predicate = hasCoOwner, coOwner = Mary)
        def onCoOwner(self, pet, predicate, coOwner):
            print(pet, hasCoOwner, coOwner)
        @opf.realizer(isCloseRelativeOf)
        def onIsCloseRelativeOf(self, **kwargs):
            print("onIsCloseRelativeOf realizer is here!")
    r = MyRealizers()
    family.realize(r)

def repeatRealizerExample():
    print("\n>>repeatRealizerExample")
    family = opf.Ontology()
    with family:
        Tiger = Cat("Tiger")
        Robert = Person("Robert")
        Mary = Person("Mary")
        hasOwner(Tiger, Robert)
        isCloseRelativeOf(Robert, Mary)
    count = 1
    @opf.realizer(hasCoOwner)
    def onCoOwner(pet, predicate, coOwner):
        nonlocal count, family
        if count == 1:
            with family:
                Oscar = Cat("Oscar")
                isCloseRelativeOf(Mary, Robert)
                hasOwner(Oscar, Mary)
        else:
            print("count:", count)
            print(pet, predicate, coOwner)
        count += 1
    family.realize(onCoOwner)

def alternativePatternsRealizerExample():
    print("\n>>alternativePatternsRealizerExample")
    family = opf.Ontology()
    with family:
        Tiger = Cat("Tiger")
        Robert = Person("Robert")
        Mary = Person("Mary")
        hasOwner(Tiger, Robert)
        isCloseRelativeOf(Robert, Mary)

    @opf.realizer(hasCoOwner, coOwner = Mary)
    @opf.realizer(isCloseRelativeOf)
    def sampleRealizer(**kwargs):
        print("sampleRealizer output: ", kwargs)

    family.realize(sampleRealizer)

realizerExample()
inClassRealizerExample()
repeatRealizerExample()
alternativePatternsRealizerExample()
